const app = getApp()
const $ = app.globalData
Page({
	data: {
		tabs: ["全部", "待取货", "待发货", "待收货", "已完成"],
		activeIndex: 0,
		page: 1,
		orderstate: "",
		orderList: [{
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}, {
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}, {
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}, {
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}, {
			list: [],
			page: 1,
			gengduo: false,
			falis: true
		}]
	},
	onLoad: function(options) {
		var that = this
		if (options && options.id != "undefined") {
			this.setData({
				activeIndex: options.id
			})
		}
		wx.getStorage({
			key: "userinfo",
			success: function(res) {
				that.setData({
					userinfo: res.data
				})
				that.getorderlist()
			}
		})
		wx.stopPullDownRefresh();
	},
		// 上拉刷新
	onPullDownRefresh() {
		var that = this
			that.setData({
				orderList: [{
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}, {
					list: [],
					page: 1,
					gengduo: false,
					falis: true
				}]
			})
			that.onLoad();
	  },
	// 获取订单
	getorderlist: function() {
		var that = this
		var expresstype = ''
		var activeIndexxx = ''
		var orderList = that.data.orderList
		switch (that.data.activeIndex) {
			case 0:
				activeIndexxx = '';
				break;
			case 1:
				activeIndexxx = 2;
				expresstype = 1;
				break;
			case 2:
				activeIndexxx = 2;
				expresstype = 0;
				break;
			case 3:
				activeIndexxx = 3;
				break;
			case 4:
				activeIndexxx = 6;
				break;
		}
		if (orderList[that.data.activeIndex].falis || that.data.shuxin) {
			console.log(123456)
			wx.request({
				url: $.AdminUrl + "/YApi/getMyOrder",
				data: {
					customerid: that.data.userinfo.id,
					name: "",
					orderstate: activeIndexxx,
					expresstype: expresstype,
					page: that.data.orderList[that.data.activeIndex].page
				},
				success: (res) => {
					wx.stopPullDownRefresh();
					var order = res.data.obj
					if(that.data.shuxin){
						orderList[that.data.activeIndex].list = order
					}else{
						orderList[that.data.activeIndex].list = orderList[that.data.activeIndex].list.concat(order)
					}
					orderList[that.data.activeIndex].falis = false
					if (order.length >= 10) {
						orderList[that.data.activeIndex].gengduo = true
					} else {
						orderList[that.data.activeIndex].gengduo = false
					}
					console.log(orderList)
					that.setData({
						orderList: orderList,
						shuxin:false
					})
					
				}
			})
		}
	},
	// 加载更多
	loadgengduo: function() {
		var that = this
		var orderList = that.data.orderList
		var page = orderList[that.data.activeIndex].page + 1
		orderList[that.data.activeIndex].page = page
		orderList[that.data.activeIndex].falis = true
		that.setData({
			page: page,
			orderList: orderList
		})
		that.getorderlist()
	},
	
	lookkuaidi: function(e) {
        var a = e.currentTarget.dataset.no, t = e.currentTarget.dataset.quhuo ? e.currentTarget.dataset.quhuo : 0;
        wx.navigateTo({
            url: "orderdetails/orderdetails?id=" + a + "&quhuo=" + t
        });
    },
	
    shouhou: function(a) {
        var t = a.currentTarget.dataset.id;
        wx.request({
            url: $.AdminUrl + "/YApi/order_aftersale",
            data: {
                orderid: t
            },
            success: function(e) {
                e.data.code ? wx.showToast({
                    title: e.data.msg,
                    duration: 3e3
                }) : wx.showToast({
                    icon: "none",
                    title: e.data.msg,
                    duration: 3e3
                });
            }
        });
    },
    shouhuo: function(a) {
		var that = this
        var t = a.currentTarget.dataset.id;
        wx.request({
            url: $.AdminUrl + "/YApi/order_finish",
            data: {
                customerid: wx.getStorageSync("userinfo").id,
                orderid: t
            },
            success: function(e) {
				that.setData({
					shuxin:true
				})
                e.data.code ? (wx.showToast({
                    title: e.data.msg,
                    duration: 3e3
                }), that.onLoad()) : wx.showToast({
                    icon: "none",
                    title: e.data.msg,
                    duration: 3e3
                });
            }
        });
    },
	bindChange: function(e) {
		var that = this
		var activeIndex = parseInt(e.currentTarget.id)
		var orderlist = that.data.orderList
		var page = orderlist[e.currentTarget.id].page
		that.setData({
			activeIndex: activeIndex,
			page: page
		});
		that.getorderlist()
	},// 分享转发
		onShareAppMessage: function() {
			var userinfo = wx.getStorageSync('userinfo')
			return {
				title: '优悦购生活管家',
				path: '/pages/index/index?userid='+userinfo.loginname,
				success: (res) => {
					console.log("转发成功", res);
				},
				fail: (res) => {
					console.log("转发失败", res);
				}
			}
		}
});
